import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class LoginJFrame extends JFrame implements ActionListener
{
    private final JComboBox<String> studentNameJComboBox;
    private final JPasswordField passwordJPasswordField;
    private final JButton loginJButton;
    private final JCheckBox saveJCheckBox;
    private final LoginActionListener action;

    public LoginJFrame(LoginActionListener action)
    {
        super("用户登录");
        this.setSize(300,300);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.action=action;

        this.setLayout(new GridLayout(3,1));

        //用户信息
        JPanel studentNameJPanel=new JPanel();
        this.add(studentNameJPanel);
        studentNameJPanel.setLayout(new GridLayout(2,2));
        studentNameJPanel.add(new JLabel("用户名"));
        studentNameJPanel.add(this.studentNameJComboBox=new JComboBox<String>());
        studentNameJPanel.add(new JLabel("密码"));
        studentNameJPanel.add(this.passwordJPasswordField=new JPasswordField());
        this.studentNameJComboBox.setEditable(true);
        //保存
        JPanel saveJPanel=new JPanel();
        this.add(saveJPanel);
        saveJPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        saveJPanel.add(this.saveJCheckBox=new JCheckBox("记住用户名"));


        JPanel buttonsJPanel=new JPanel();
        this.add(buttonsJPanel);
        buttonsJPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        buttonsJPanel.add(this.loginJButton=new JButton("登录"));
        this.loginJButton.addActionListener(this);

        this.setVisible(true);
    }

    public void addHistory(String name)
    {
        this.studentNameJComboBox.addItem(name);
    }

    public void actionPerformed(ActionEvent e)
    {
        if(this.action!=null)
            this.action.loginAction((String)this.studentNameJComboBox.getSelectedItem(),String.valueOf(this.passwordJPasswordField.getPassword()),this.saveJCheckBox.isSelected());
    }
}
